package com.duolaibei.business.view.dialog;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseDialog;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.model.ShopListData;
import com.duolaibei.business.model.WorkerData;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.activity.SearchOrderActivity;
import com.duolaibei.business.view.activity.ShopEditActivity;
import com.duolaibei.business.view.activity.TerminalEditorActivity;
import com.duolaibei.business.view.activity.SearchTerminalActivity;
import com.duolaibei.business.widget.loopview.LoopView;
import com.duolaibei.business.widget.loopview.OnItemSelectedListener;
import com.zhy.adapter.recyclerview.CommonAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/11/7 0007
 */

public class ShopDialog extends BaseDialog {

    @Inject(R.id.shop_choose_loopview)
    private LoopView mLoopView;
    @Inject(R.id.choose_cancle)
    private TextView mTextViewCancle;
    @Inject(R.id.choose_confirm)
    private TextView mTextViewConfirm;
    @Inject(R.id.shop_list_recycle)
    private RecyclerView mRecyclerView;
    private CommonAdapter<ShopListData.DataBeanX.DataBean> mCommonAdapter;
    private List<ShopListData.DataBeanX.DataBean> mBeanList;//门店列表
    private List<WorkerData.DataBeanX.DataBean> mWorkerList;//员工列表
    private String name;
    private String id;
    private Context mContext;

    private static int type;

    public ShopDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.dialog_shop);
    }

    @Override
    protected void initViews() {
        initWindow(1.0f, 0.45f, Gravity.BOTTOM);
        setAnimate(R.style.animatedialog);
    }

    @Override
    protected void onSetListener() {
        mTextViewCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mTextViewConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (name == null) {
                    if (type == 1) {
                        name = mBeanList.get(2).getName();
                    } else if (type == 2) {
                        name = mWorkerList.get(2).getWorkmen_uid();
                    }
                }
                if (id == null) {
                    if (type == 1) {
                        id = mBeanList.get(2).getWorkmen_uid();
                    } else if (type == 2) {
                        id = mWorkerList.get(2).getWorkmen_uid();
                    }
                }
                mShopDialogListener.onItemSelected(name, id);
                dismiss();
//                }
            }
        });
    }

    /**
     * 获取已经选中的item
     *
     * @return
     */
    private ShopListData.DataBeanX.DataBean getSelecedItem() {
        for (ShopListData.DataBeanX.DataBean dataBean : mBeanList) {
            if (dataBean.isSelected()) {
                return dataBean;
            }
        }
        return null;
    }

    /**
     * 点击回调 name , id
     */
    private ShopDialogListener mShopDialogListener;

    public void setShopDialogListener(ShopDialogListener mShopDialogListener) {
        this.mShopDialogListener = mShopDialogListener;
    }

    public static class ShopDialogListener {
        public void onItemSelected(String name, String id) {

        }
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        if (mContext instanceof SearchOrderActivity
                || mContext instanceof SearchTerminalActivity
                || mContext instanceof TerminalEditorActivity
                ) {
            getShopList();//门店列表
            type = 1;
        } else if (mContext instanceof ShopEditActivity) {
            getWorkerData();//员工列表
            type = 2;
        }
//        setShopAdapter();
    }

    /**
     * 设置滚轮门店
     *
     * @param mBeanList
     */
    private void setLoopView(final List<ShopListData.DataBeanX.DataBean> mBeanList) {
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < mBeanList.size(); i++) {
            strings.add(mBeanList.get(i).getName());
        }
        mLoopView.setItems(strings);
        mLoopView.setNotLoop();//设置是否循环播放
        mLoopView.setInitPosition(2);//设置初始位置
        mLoopView.setTextSize(17);//设置字体大小
        mLoopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                name = strings.get(index);
                id = mBeanList.get(index).getId();
            }
        });
    }

    /**
     * 设置滚轮员工列表
     *
     * @param mBeanList
     */
    private void setWorkerLoopView(final List<WorkerData.DataBeanX.DataBean> mBeanList) {
        final List<String> strings = new ArrayList<>();
        for (int i = 0; i < mBeanList.size(); i++) {
            strings.add(mBeanList.get(i).getWorkmen_uid());
        }
        mLoopView.setItems(strings);
        mLoopView.setNotLoop();//设置是否循环播放
        mLoopView.setInitPosition(2);//设置初始位置
        mLoopView.setTextSize(17);//设置字体大小
        mLoopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                name = strings.get(index);
                id = mBeanList.get(index).getId();
            }
        });
    }

    /**
     * 初始化数据 设置第一个为选中状态
     */
    private void initBeanList() {
        for (int i = 0; i < mBeanList.size(); i++) {
            if (i == 0) {
                mBeanList.get(i).setSelected(true);
            } else {
                mBeanList.get(i).setSelected(false);
            }
        }
    }

    /**
     * 点击改变数据
     */
    private void setBeanList(ShopListData.DataBeanX.DataBean dataBean) {
        for (int i = 0; i < mBeanList.size(); i++) {
            if (dataBean.getId().equals(mBeanList.get(i).getId())) {
                mBeanList.get(i).setSelected(true);
            } else {
                mBeanList.get(i).setSelected(false);
            }
        }
        mCommonAdapter.notifyDataSetChanged();
    }


    /**
     * 获取全部门店列表
     */
    private void getShopList() {
        mBeanList = new ArrayList<>();
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.SHOPLIST);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", String.valueOf(PreferenceUtil.getParam(Preferences.LOGINTYPE, "")));
        param.add("current_page", "1");//在一页返回全部门店
        param.add("page_size", "500");//每页显示500条数据
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getThis().getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        ShopListData data = GsonUtil.GsonToBean(string, ShopListData.class);
                        mBeanList.clear();
                        mBeanList.addAll(data.getData().getData());
                        getThis().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mBeanList.size() != 0) {
                                    setLoopView(mBeanList);
                                } else {
                                    showToast("暂无数据");
                                }
                            }
                        });
                    } else {
                        showToast(result.getInfo());
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }

    /**
     * 获取全部员工列表
     */
    private void getWorkerData() {
        mWorkerList = new ArrayList<>();
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.WORKMEN);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", "merchant");//该接口只能商家登录访问
        param.add("current_page", "1");
        param.add("page_size", "500");
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getThis().getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
                        WorkerData data = GsonUtil.GsonToBean(string, WorkerData.class);
                        mWorkerList.clear();
                        mWorkerList.addAll(data.getData().getData());
                        getThis().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (mWorkerList.size() != 0) {
                                    setWorkerLoopView(mWorkerList);
                                } else {
                                    showToast("暂无数据");
                                }
                            }
                        });
                    } else {
                        showToast(result.getInfo());
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
}
